package entity;

import java.util.ArrayList;
import java.util.HashMap;

/**
 * 
 * The world containing all the entities.
 * 
 * The function of this class is simply to store all the entities and give the 
 * programmer an easy access to every entities.
 * 
 * @author Gudradain
 *
 */
public class World {
	
	private final HashMap<String, ArrayList<Entity>> entityListMap = new HashMap<String, ArrayList<Entity>>();
	
	/**
	 * Add an entity to this world.
	 * 
	 * @param entity
	 */
	public void addEntity(Entity entity){
		ArrayList<Entity> entityList = entityListMap.get(entity.getID());
		if(entityList == null){
			entityList = new ArrayList<Entity>();
			entityListMap.put(entity.getID(), entityList);
		}
		entityList.add(entity);
		
		for(Entity module : entity.getModuleList()){
			ArrayList<Entity> moduleList = entityListMap.get(entity.getID());
			if(moduleList == null){
				moduleList = new ArrayList<Entity>();
				entityListMap.put(module.getID(), moduleList);
			}
			moduleList.add(module);
		}
	}
	
	/**
	 * Remove an entity from this world.
	 * 
	 * @param entity
	 */
	public void removeEntity(Entity entity){
		ArrayList<Entity> entityList = entityListMap.get(entity.getID());
		if(entityList != null){
			entityList.remove(entity);
		}
		
		for(Entity module : entity.getModuleList()){
			ArrayList<Entity> moduleList = entityListMap.get(entity.getID());
			if(moduleList != null){
				moduleList.add(module);
			}
		}
	}
	
	/**
	 * Get all the entity of this sort in this world.
	 * 
	 * The entities that you will get will probably not be of the type entityID
	 * because it will return all the entities having that sort of entities
	 * within them.
	 * 
	 * @param entityID
	 * @return
	 */
	public ArrayList<Entity> getEntityList(String entityID){
		ArrayList<Entity> newEntityList = null;
		
		ArrayList<Entity> entityList = entityListMap.get(entityID);
		if(entityList != null){
			newEntityList = new ArrayList<Entity>();
			for(Entity entity : entityList){
				newEntityList.add(entity);
			}
		}
		return entityList;
	}

}
